/**
 * @file AsignarResultadoPruebaRadiografia.java
 * @brief Interfaz asigna el tipo de radiografia
 *
 * Copyright (C)  - May [2011]
 *
 * @author Carlos Castilla Ruiz & Antonio M. Gutierrez Martinez
 * @version 01.00
 *
 */

package interfaces;

import clinica.ClinicaView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JPanel;
import org.jdesktop.application.Action;
import clinica.Interfaces;
import GestorPruebas.*;
import Controlador.Controlador;
import java.io.IOException;
import java.sql.SQLException;
import GestorUsuarios.Paciente;
import clinica.Interfaz;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;



public class AsignarResultadoPruebaRadiografia extends javax.swing.JPanel {

    private ClinicaView app;
    Interfaces _anterior;
    private Radiografia prueba;

    public AsignarResultadoPruebaRadiografia(ClinicaView a) {
        initComponents();
        app = a;
    }


    public void limpiaEntradas(Prueba p,Interfaces ant) throws java.security.NoSuchAlgorithmException,java.lang.ClassNotFoundException,java.io.IOException,java.sql.SQLException{
       _anterior=ant;
       prueba= (Radiografia)p;
       DateFormat formato_fecha = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

       if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.A ) {
                nombre_prueba.setText("Radiografía de abdomen");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.CL ) {
                nombre_prueba.setText("Radiografía de columna");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.CR ) {
                nombre_prueba.setText("Radiografía de cráneo");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.H ) {
                nombre_prueba.setText("Radiografía de hombro");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.ID ) {
                nombre_prueba.setText("Radiografía de extremidad inferior derecha");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.II ) {
                nombre_prueba.setText("Radiografía de extremidad inferior izquierda");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.SD ) {
                nombre_prueba.setText("Radiografía de extremidad superior derecha");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.SI ) {
                nombre_prueba.setText("Radiografía de extremidad superior izquierda");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.T ) {
                nombre_prueba.setText("Radiografía de torax");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.TC ) {
                nombre_prueba.setText("Radiografía telemática de columna");
            }
            else if( ((Radiografia)p).getTipoRadiografia() == TipoRadiografia.TM ) {
                nombre_prueba.setText("Radiografía telemática MII");
            }
      
       
       Paciente pac = (Paciente) Paciente.get(prueba.getIdPaciente());
       informacion_prueba.setText(pac.getNombre()+" "+pac.getApellidos()+", "+formato_fecha.format(prueba.getFecha()));

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        nombre_prueba = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        informacion_prueba = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos de la prueba", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setFont(Interfaz.setFontSize());
        jLabel1.setText("Tipo Radiografía:");
        jLabel1.setName("jLabel1"); // NOI18N

        nombre_prueba.setEditable(false);
        nombre_prueba.setFont(Interfaz.setFontSize());
        nombre_prueba.setName("nombre_prueba"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(AsignarResultadoPruebaRadiografia.class, this);
        jButton3.setAction(actionMap.get("subirRadiografia")); // NOI18N
        jButton3.setFont(Interfaz.setFontSize());
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(AsignarResultadoPruebaRadiografia.class);
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText("Subir imagen");
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 51, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 363, Short.MAX_VALUE)
        );

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 363, Short.MAX_VALUE)
        );

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText("Paciente:");
        jLabel2.setName("jLabel2"); // NOI18N

        informacion_prueba.setFont(Interfaz.setFontSize());
        informacion_prueba.setName("informacion_prueba"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(84, 84, 84)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nombre_prueba, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(informacion_prueba, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE))
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(informacion_prueba, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 104, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(nombre_prueba, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(58, 58, 58)
                        .addComponent(jButton3)
                        .addGap(59, 59, 59))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        jButton2.setAction(actionMap.get("cancelar")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
 @Action
    public void aceptar() throws java.text.ParseException,GestorUsuarios.IDException,java.security.NoSuchAlgorithmException,java.sql.SQLException,java.io.IOException,java.lang.ClassNotFoundException {
       app.limpiaErrores();
       String exito=null;
        
            prueba.setEstadoPrueba(EstadoPrueba.Realizada);
            exito=Controlador.cambiarEstadoPrueba(prueba.getIdPrueba(),EstadoPrueba.Realizada);

            if ( exito == null ) {
                exito=Controlador.asignarResultados(prueba);
                if(exito==null) {
                    app.abrirMensajeExito("Los resultados han sido asignados correctamente", Interfaces.PRINCIPAL_TECNICO);
                } else {
                    app.abrirMensajeError(exito,_anterior);
                }
            }else
                app.abrirMensajeError(exito, _anterior);

        
    }

    @Action
    public void subirRadiografia() throws GestorUsuarios.IDException,java.lang.ClassNotFoundException,java.io.IOException,java.sql.SQLException,java.security.NoSuchAlgorithmException{
        File selectedFile = null;
        String rutaAbsoluta;
        JFileChooser fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Radiografia", "png");
        fileChooser.setFileFilter(filter);
        fileChooser.setDialogTitle("Subir radiografia");
        int status = fileChooser.showSaveDialog(null);// Da un entero

        // Si apretamos en aceptar ocurrirá esto
        if (status == JFileChooser.APPROVE_OPTION) {
            selectedFile = fileChooser.getSelectedFile();
            rutaAbsoluta = selectedFile.getParent();
            //System.out.println(rutaAbsoluta);
            //comprueba que contenga .png
            //Pattern p = Pattern.compile(".png$");
            //Matcher m = p.matcher(selectedFile.getAbsolutePath());

         
            rutaAbsoluta += "\\";
            
            String nombre=selectedFile.getName();

            Imagen imag= new Imagen(rutaAbsoluta,nombre,prueba);
            imag.add(imag);

           
        }
    }

    @Action
    public void cancelar() {
        app.volver(_anterior);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel informacion_prueba;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField nombre_prueba;
    // End of variables declaration//GEN-END:variables

}
